<?php

/**
 * ContactForm class.
 * ContactForm is the data structure for keeping
 * contact form data. It is used by the 'contact' action of 'SiteController'.
 */
class Hyip extends ActiveRecord
{
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function tableName()
	{
		return 'vh_hyip';
	}
	
	public function rules()
	{
		return array(
		);
	}

	public function search()
	{
		$condition = "";
		$conditionJoin = "";
		
		if(!empty($_POST['domain']))
			$condition .= " AND domain LIKE '%" . $_POST['domain'] . "%'";
		
		if(!empty($_POST['ip']))
			$condition .= " AND ip LIKE '%" . $_POST['ip'] . "%'";
		
		if(!empty($_POST['status']))
			$condition .= " AND status = '" . $_POST['status'] . "'";
                
		if(!empty($_POST['planType']))
			$condition .= " AND planType = '" . $_POST['planType'] . "'";
		
                if(!empty($_POST['hostCountry']))
			$condition .= " AND hostCountry = '" . $_POST['hostCountry'] . "'";
                
                if(!empty($_POST['monitor']))
			$condition .= " AND monitor = '" . $_POST['monitor'] . "'";
                
		$query = "SELECT t.id, t.status, t.ip, t.domain, t.rate, t.planType, t.planTime, t.profitType, t.plan, t.budget, 
                        t.startDate, t.endDate, t.lifeTime, t.monitor, t.adv, t.payment, t.feature, t.hostCountry,
                        DATEDIFF(NOW(), t.lastPaidDate) as lastPaid, DATEDIFF(NOW(), t.lastWithdrawDate) as lastWithdraw, date(t.joinedDate) as joinedDate, 
                        IF(t.joinedDate IS NULL,0, IF( t.endDate IS NULL, DATEDIFF(NOW(), t.joinedDate), DATEDIFF(t.endDate, t.joinedDate))) as joinedDateNumber
			FROM vh_hyip t $conditionJoin
			WHERE 1 = 1 $condition";
		
		$result = $this -> executeAllTotal($query);
	
		return $result;
	}
        
        public function getItem($id)
	{
            $query = '
                    SELECT t.id, t.status, t.ip, t.domain, t.rate, t.planType, t.planTime, t.profitType, t.plan, t.budget, 					
                    t.startDate, t.endDate, IF(t.endDate IS NULL,DATEDIFF(now(), t.startDate), DATEDIFF(t.endDate, t.startDate)) as duration,
                    t.monitor, t.adv, t.payment, t.feature, t.hostCountry,
                    DATEDIFF(NOW(), t.lastPaidDate) as lastPaid, DATEDIFF(NOW(), t.lastWithdrawDate) as lastWithdraw, date(t.joinedDate) as joinedDate, 
                    IF(t.joinedDate IS NULL,0, IF( t.endDate IS NULL, DATEDIFF(NOW(), t.joinedDate), DATEDIFF(t.endDate, t.joinedDate))) as joinedDateNumber
                    FROM vh_hyip t WHERE id = 
            ' . $id;
            
            $result = $this -> executeRow($query);

            return $result;
	}
        
	public function getItemsByStatus($status = "Invest")
	{
            $query = '
                    SELECT id, domain
                    FROM vh_hyip 
                    WHERE status = :status';
            
            $result = $this -> executeAll($query, array(':status' => $status), false);

            return $result;
	}             
    
	public function getActiveDomain()
	{
            $query = '
                    SELECT id, domain
                    FROM vh_hyip 
                    WHERE status = "Invest" OR status = "Monitor"';
            
            $result = $this -> executeAll($query, array(), false);

            return $result;
	}   
	
        public function getMonitor($hyipID)
	{
            $query = "SELECT m.id, m.domain, hm.hyipID
                FROM vh_monitor m
                JOIN vh_hyip_monitor hm ON hm.monitorID = m.id AND hm.hyipID = $hyipID;
            ";

            $result = $this -> executeAllTotal($query);

            return $result;
	}
        
        public function addMonitor($hyipID, $monitorID)
	{
            try{
                $query = "INSERT INTO vh_hyip_monitor(hyipID, monitorID) VALUES ($hyipID, $monitorID)";
                $this ->executeNone($query);

                return true;
            }catch(Exception $ex)
            {
                return false;
            }
	}
        
        public function deleteMonitor($hyipID, $monitorID)
	{
            $query = "DELETE FROM vh_hyip_monitor WHERE hyipID = $hyipID AND monitorID = $monitorID";
            
            $result = $this ->executeNone($query);

            return $result;
	}
        
        public function deleteHyip($id)
        {
            $item = Hyip::model() -> findByPk($id);
             if ($item -> delete()) {
                 
                 $query = "DELETE FROM vh_hyip_monitor WHERE hyipID = :hyipID";
                 $this ->executeNone($query, array(":hyipID" => $id));
                 
                 return true;
             }
             else{
                 return false;
             }
        }
		
	public function updateBudget($hyipID)
	{
		$result = 0;
		if($hyipID > 0)
		{
            $query = "UPDATE vh_hyip
                    SET budget = (SELECT SUM(cost) as cost
                    FROM vh_budget
                    WHERE hyipId = $hyipID)
                    WHERE id = $hyipID
            ";
            
            $result = $this ->executeNone($query);
		}
		
        return $result;
	}
        
        public function refreshLifeTime()
	{		
            $query = "
                UPDATE vh_hyip 
                SET lifeTime = IF(endDate IS NULL,DATEDIFF(now(), startDate),DATEDIFF(endDate, startDate))
            ";
            
            $result = $this ->executeNone($query);
		
            return true;
	}   
        
        public function updateLifeTime($id)
	{		
            $query = "
                UPDATE vh_hyip
                SET lifeTime = IF(endDate IS NULL,DATEDIFF(now(), startDate),DATEDIFF(endDate, startDate))
                WHERE id = $id
            ";

            $result = $this ->executeNone($query);
            
            return true;
	}    
}